from django.shortcuts import render
from apps.articles.models import *
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.views import APIView
from rest_framework import viewsets
from django_filters import *
from rest_framework import filters
from apps.articles.serializer import *

from .pagination import *



def base(request):
    return render(request, 'base.html')

class ProductsViewSet(viewsets.ModelViewSet):
    """
        list() 提供一组数据
        retrieve() 提供单个数据
        create() 创建数据
        update() 修改数据
        destory() 删除数据
        """
    # 数据，必须定义一个默认排序规则，否则报错
    queryset = Products.objects.all().order_by('id')
    # 分页
    pagination_class = ProductsPagination
    # 序列化
    serializer_class = ProductsSerializer
    # 过滤
    # filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    # filter_class = GoodsFilters
    # 搜索
    search_fields = ('name')
    # 排序
    ordering_fields = ('id')

class TagsViewSet(viewsets.ModelViewSet):
    """
        list() 提供一组数据
        retrieve() 提供单个数据
        create() 创建数据
        update() 修改数据
        destory() 删除数据
        """
    # 数据，必须定义一个默认排序规则，否则报错
    queryset = Tags.objects.all().order_by('id')
    # 分页
    pagination_class = TagsPagination
    # 序列化
    serializer_class = TagsSerializer
    # 过滤
    # filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    # filter_class = GoodsFilters
    # 搜索
    search_fields = ('name')
    # 排序
    ordering_fields = ('id')

class VersionsViewSet(viewsets.ModelViewSet):
    """
        list() 提供一组数据
        retrieve() 提供单个数据
        create() 创建数据
        update() 修改数据
        destory() 删除数据
        """
    # 数据，必须定义一个默认排序规则，否则报错
    queryset = Versions.objects.all().order_by('id')
    # 分页
    pagination_class = VersionsPagination
    # 序列化
    serializer_class = VersionsSerializer
    # 过滤
    # filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    # filter_class = GoodsFilters
    # 搜索
    search_fields = ('name')
    # 排序
    ordering_fields = ('id')


class DocumentViewSet(viewsets.ModelViewSet):
    """
    list() 提供一组数据
    retrieve() 提供单个数据
    create() 创建数据
    update() 修改数据
    destory() 删除数据
    """
    # 数据，必须定义一个默认排序规则，否则报错
    queryset = Document.objects.all().order_by('-update_time')
    # 分页
    pagination_class = DocumentPagination
    # 序列化
    serializer_class = DocumentSerializer
    # 过滤
    # filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    # filter_class = GoodsFilters
    # 搜索
    search_fields = ('title', 'product', 'tags')
    # 排序
    ordering_fields = ('product', 'update_time')


class FAQViewSet(viewsets.ModelViewSet):
    """
    list() 提供一组数据
    retrieve() 提供单个数据
    create() 创建数据
    update() 修改数据
    destory() 删除数据
    """
    # 数据，必须定义一个默认排序规则，否则报错
    queryset = FAQ.objects.all().order_by('-update_time')
    # 分页
    pagination_class = FAQPagination
    # 序列化
    serializer_class = FAQSerializer
    # 过滤
    # filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    # filter_class = GoodsFilters
    # 搜索
    search_fields = ('title', 'product', 'tags')
    # 排序
    ordering_fields = ('product', 'update_time')


class ToolsViewSet(viewsets.ModelViewSet):
    """
    list() 提供一组数据
    retrieve() 提供单个数据
    create() 创建数据
    update() 修改数据
    destory() 删除数据
    """
    # 数据，必须定义一个默认排序规则，否则报错
    queryset = FAQ.objects.all().order_by('-update_time')
    # 分页
    pagination_class = ToolsPagination
    # 序列化
    serializer_class = ToolsSerializer
    # 过滤
    # filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    # filter_class = GoodsFilters
    # 搜索
    search_fields = ('title', 'product', 'tags')
    # 排序
    ordering_fields = ('product', 'update_time')